package com.juzhencms.apps.busi.service;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.juzhencms.apps.base.common.database.DBTable;
import com.juzhencms.apps.base.common.util.ObjectUtil;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class SiteService extends CommonService{
	

	@Autowired
	private UserService userService;
	
	public String getLink(Map site, HttpServletRequest request) {
		// TODO Auto-generated method stub
		String link=request.getScheme()+"://"+site.get("domain");
		if(request.getServerPort()!=80)
			link+=":"+request.getServerPort();
		link+=request.getContextPath();
		return link;
	}
	

	/*
	 * getLists：获取(受机构限制)站点列表
	 * @param fields 查询字段默认
	 * @param where String 条件，比如：name like ?
	 * @param whereParamList list 条件参数，如：List whereParamList=new ArrayList();whereParamList.add("%同仁堂%");
	 * @param orderBy 排序 默认 order by position asc,id asc
	 * @param pageIndex 页码
	 * @param pageSize 每页页数 使用时需与pageIndex都设置 
	 * @param is_not_limit_org 不设置机构限制 默认0设置 1不设置
	 * @return list
	 * 应用：siteService.getAllSite(null, null, null, null, 0, 0,0);
	 */
	public List getLists(String fields, String where, List whereParamList, String orderBy, int pageIndex,
			int pageSize,int is_not_limit_org) {

		// 查询字段
		if (fields == null || fields.equals("")) {
			fields = " * ";
		}

		// 查询条件
		String setWhere = "  where  1=1  ";
		if (where != null && !where.equals("")) {
			setWhere += " and "+where;
		}
		
		//机构限制：系统管理员、超级管理员以外的管理员用户都需要加机构限制
		if(is_not_limit_org == 0) {
			String getLoginUserOrgIds = userService.getLoginUser().getSubOrgIds();
			/*if( userService.getLoginUserPermissions().get("root")==null && !getLoginUserOrgIds.equals("")) {
				setWhere += " and org_id in ("+getLoginUserOrgIds+") ";
			}*/
			setWhere += " and org_id in ("+getLoginUserOrgIds+") ";
		}
		
		// 排序：默认ID倒序
		if (orderBy == null || orderBy.equals("")) {
			orderBy = " order by position asc,id asc";
		}

		// 页码，0代表全显示
		String setLimit = "";
		if (pageIndex > 0 && pageSize > 0) {
			setLimit = " limit ?,? ";
			//
			int start=0;
			if(pageIndex>1){
				start=(pageIndex-1)*pageSize;
			}
			whereParamList.add(start);
			whereParamList.add(pageSize);
		}

		// 语句
		String sql = "select " + fields + " from " + DBTable.site + setWhere + orderBy + setLimit;

		// 参数条件
		List dataList = null;
		if (whereParamList != null && whereParamList.size() > 0) {
			dataList = this.getJdbcDao().querySql(sql, whereParamList.toArray());
		} else {
			dataList = this.getJdbcDao().querySql(sql);
		}
		return dataList;

	}
	
	/*
	 * 获取(受机构限制)站点列表
	 */
	public List getLists(String fields) {
		// 查询字段
		if (fields == null || fields.equals("")) {
			fields = " * ";
		}
		return this.getLists(fields, null, null, null, 0, 0,0);
	}
	
	/*
	 * getDetail：获取（机构限制）指定站点信息
	 * @param fields 查询字段默认
	 * @param where String 条件，比如：name like ?
	 * @param whereParamList list 条件参数，如：List whereParamList=new ArrayList();whereParamList.add("%机构%");
	 * @param orderBy String 排序
	 * @param is_not_limit_org 不设置机构限制 默认0设置 1不设置
	 * @return Map
	 */
	public Map getDetail(String fields, String where, List whereParamList,String orderBy,int is_not_limit_org) {
		
		// 查询字段
		if (fields == null || fields.equals("")) {
			fields = " * ";
		}

		// 查询条件
		String setWhere = "  where  del_flag=0  ";
		if (where != null && !where.equals("")) {
			setWhere += " and "+where;
		}
		
		//机构限制：系统管理员、超级管理员以外的管理员用户都需要加机构限制
		if(is_not_limit_org == 0) {
			String getLoginUserOrgIds = userService.getLoginUser().getSubOrgIds();
			/*if( userService.getLoginUserPermissions().get("root")==null && !getLoginUserOrgIds.equals("")) {
				setWhere += " and org_id in ("+getLoginUserOrgIds+") ";
			}*/
			setWhere += " and org_id in ("+getLoginUserOrgIds+") ";
		}

		// 排序：默认ID倒序
		if (orderBy == null) {
			orderBy = " order by position asc,id asc";
		}

		// 仅查询一条
		String setLimit = " limit 1 ";
		
		// 语句
		String sql = "select " + fields + " from " + DBTable.site + setWhere + orderBy + setLimit;

		// 参数条件
		Map data = new HashMap();
		if (whereParamList != null && whereParamList.size() > 0) {
			data =  this.getJdbcDao().queryRow(sql,whereParamList.toArray());	
		} else {
			data =  this.getJdbcDao().queryRow(sql);	
		}
		return data;
	}
	
	/*
	 * getDetail：获取（机构限制）指定站点信息
	 */
	public Map getDetail(int id,String fields) {
		if(id<=0) {
			return null;
		}
		String where = " id =  "+id;
		return this.getDetail(fields, where,null,null,0);
	}
	
	

}
